<template>
  <div>
    <!-- 2.1分页组件使用 ,文档下面有英文说明属性或事件等-->
    <div class="block">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        :page-size="pageSize"
        :page-sizes="[2, 5, 8]"
        @size-change="SizeChange"
        @current-change="CurrentChange"
      ></el-pagination>
    </div>

    <!-- 1.1表格组件的使用 :data是存表格数据的地方-->

    <el-table :data="tableData" style="width: 100%">
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <!-- 1.2自定义列没有prop,需要利用 slot-scoped 将:data="tableData"遍历的数据 item 接回来 -->
      <el-table-column label="缩略图" width="234">
        <template slot-scope="scoped">
          <!--两个代表的意思 item===scoped.row  index===scoped.$index-->
          <img
            v-if="scoped.row.cover.length > 0"
            :src="scoped.row.cover[0].url | fixUrl"
            alt
            class="thumbnail"
          />
          <img v-else src="../images/u4.png" alt class="thumbnail" />
        </template>
      </el-table-column>
      <el-table-column prop="title" label="图像" width="380"></el-table-column>
      <el-table-column
        prop="user.nickname"
        label="作者"
        width="180"
      ></el-table-column>
      <el-table-column label="修改" width="180">
        <template slot-scope="scoped">
          <el-button
            type="primary"
            @click="$router.push('/PublishArticle?id=' + scoped.row.id)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 2.2底部也来个分页组件 -->
    <div class="block">
      <el-pagination
        layout="total,  prev, pager, next, jumper"
        :total="total"
        :page-size="pageSize"
        :page-sizes="[2, 5, 8]"
        @size-change="SizeChange"
        @current-change="CurrentChange"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      tableData: [],
      total: 0,
      // 2.1.1
      pageSize: 5, //每个页面有多少条
      pageIndex: 1, //当前是第几页面
    };
  },
  //监听图像url,是否有http,没有帮其加上去 使用的地方用 |fixUrl
  filters: {
    fixUrl (url) {
      const reg = /^http/;
      if (reg.test(url)) {
        return url;
      } else {
        return "http://127.0.0.1:3000" + url;
      }
    },
  },
  methods: {
    // 监听分页组件的事件
    SizeChange (pageSize) {
      this.pageSize = pageSize;
    },
    CurrentChange (pageIndex) {
      this.pageIndex = pageIndex;
      this.LoadPage();
    },
    LoadPage () {
      //所有文章在一起,id传不传都可以
      this.$axios({
        url: "/post",
        params: {
          //2.1.2设置当前页面,条
          pageSize: this.pageSize,
          pageIndex: this.pageIndex,
        },
      }).then((res) => {
        console.log(res.data.data);
        this.tableData = res.data.data;
        this.total = res.data.total;
      });
    },
  },

  mounted () {

  },
};
</script>

<style lang="less" scoped>
.thumbnail {
  width: 234px;
  height: 146px;
  object-fit: cover;
}
</style>